<?php
namespace Ptxq\Controller;
use Common\Controller\AdminbaseController;

class IndexAdminController extends AdminbaseController{

		function index(){
			$Ptxq=M("Ptxq");
			$count= $Ptxq->count();// 查询满足要求的总记录数		
			$Page = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show = $Page->show();// 分页显示输出
			$result=$Ptxq->order('sort asc')->limit($Page->firstRow.','.$Page->listRows)->select();
			foreach($result as $key=>$v){
				$id=$v['id'];
				$list = $Ptxq->table('cmf_pt_pz ptpz, cmf_jypz jypz')->where('ptpz.pt_id = '.$id.' and ptpz.pz_id =jypz.id')->field('jypz.jypz,jypz.id')->select();
				$result[$key]['child']=$list;
				
			}

		//	dump($result);die();
		
			$this->assign('ptxq',$result);
			$this->assign('page',$show);// 赋值分页输出		
		 $this->display();
		}
		function add(){
			if($_POST){
				$Ptxq=M("Ptxq");
				$result=$Ptxq->add($_POST);
				if ($result) {
					$this->success("添加成功！");
				} else {
					$this->error("添加失败！");
				}
			}else{
				  $this->display();
			}
		
		}
		function edit(){
				IF($_POST){
					$Ptxq=M("Ptxq");
					$result=$Ptxq->save($_POST);
					$this->success("修改成功！");
				}else{
					$Ptxq=M("Ptxq");
					$id=I('get.id');
					$result=$Ptxq->find($id);				
					$this->assign('rs',$result);
					$this->display();
				}
			}
		 
		
		function del(){
		 if($_GET){
				$id=I('id');
				$Ptxq=M("Ptxq");
				$Ptxq->delete($id);
				$this->success("删除成功！");
			}
		}
		function order(){
		 if($_POST){
				$Ptxq=M("Ptxq");
				foreach($_POST as $id=>$sort){
					$Ptxq->where(array('id'=>$id))->setField('sort',$sort);
				}
				$this->success("排序成功！");
			}
		}
		function status(){
			if($_GET){
				$Ptxq=M("Ptxq");
				if($_GET['status']==1){
					$status=0;
					$id=$_GET['id'];
					$Ptxq->where(array('id'=>$id))->setField('status',$status);
					$this->success("关闭成功！");
				}else{
					$status=1;
					$id=$_GET['id'];
					$Ptxq->where(array('id'=>$id))->setField('status',$status);
					$this->success("开启成功！");
				}
				
			}
		}
		
		function tuijian(){
			if($_GET){
				$Ptxq=M("Ptxq");
				if($_GET['tuijian']==1){
					$tuijian=0;
					$id=$_GET['id'];
					$Ptxq->where(array('id'=>$id))->setField('tuijian',$tuijian);
					$this->success("修改成功！");
				}else{
					$tuijian=1;
					$id=$_GET['id'];
					$Ptxq->where(array('id'=>$id))->setField('tuijian',$tuijian);
					$this->success("修改成功！");
				}
				
			}
		}
		//添加产品
		function add_pz(){
			 	if($_POST){
			 		$jypz=M("jypz");
					$result=$jypz->add($_POST);
					$data['pt_id']=$_POST['pt_id'];
					$data['pz_id']=$result;
					$pt_pz=M("pt_pz");
					$pt_pz->add($data);
					
					$this->success("添加成功！");
				}else{
					$Ptxq=M("Ptxq");
					$id=I('get.id');
					$result=$Ptxq->find($id);	
					$this->assign('rs',$result);
					$this->display();
				}
		 }
		
		function add_pzsx(){
			$jypzsx=M("jypzsx");
			if($_POST['id']){
			 	$result=$jypzsx->save($_POST);
				$this->success("修改成功！");
			}else{
				$result=$jypzsx->add($_POST);
				$this->success("添加成功！");
			}
		}	
		function del_pzsx(){
			if($_GET){
				$id=I('id');
				$jypzsx=M("jypzsx");
				$jypzsx->delete($id);
				$this->success("删除成功！");
			}
		}
		function edit_pz(){
			if($_POST){
				
				$data=I('post.');
				foreach($data as $k=>$v){
					if(is_numeric($k)){
						$arr[$k]=$v;
					}
					if(is_int(strpos($k,'sort_'))){
						$k=str_replace('sort_','',$k);
						$arr2[$k]=$v;
					}
				}
				
				$jypzsx=M("jypzsx");
				foreach($arr as $k=>$v){
					$sx_v['id']=$k;
					$sx_v['sx_value']=$v;
					$jypzsx->save($sx_v);
				}
				foreach($arr2 as $k=>$v){
					$sx_s['id']=$k;
					$sx_s['sort']=$v;
					$jypzsx->save($sx_s);
				}
				$jypz=M("jypz");
				$result=$jypz->save($data);
				
				$this->success("修改成功！");
				
				
			}else{
				$Ptxq=M("Ptxq");
				$jypz=M("jypz");
				$jypzsx=M("jypzsx");
				$pt_id=I('get.pt_id');
				$pz_id=I('get.pz_id');
				$sx_id=I('get.sx_id');
				$Ptxq=$Ptxq->find($pt_id);
				$jypz=$jypz->find($pz_id);
				$jypzsx_id=$jypzsx->find($sx_id);
				$jypzsx=$jypzsx->where(array('jypz_id'=>$pz_id))->order(sort)->select();
				$this->assign('jypzsx_id',$jypzsx_id);
				$this->assign('jypzsx',$jypzsx);
				$this->assign('jypz',$jypz);
				$this->assign('Ptxq',$Ptxq);
				$this->display();
			}
		}
		
		function del_pz(){
			if($_GET){
				$id=I('id');
				$pt_pz=M("pt_pz");
				$jypz=M("jypz");
				$pt_pz->where(array("pz_id"=>$id))->delete();
				$jypz->delete($id);
				$this->success("删除成功！");
				
			}
		}
		function index_yh(){
			$khyh=M('khyh');
			$rs=$khyh->select();
			$this->assign('khyh',$rs);
			$this->display();
		}
		function add_yh(){
			if($_POST){
				$khyh=M('khyh');
				$data=I('post.');
				$khyh->add($data);
				$this->success("添加成功！");
			}else{
				$this->display();
			}
				
		}
		function edit_yh(){
			IF($_POST){
					$khyh=M('khyh');
					$data=I('post.');
					$result=$khyh->save($data);
					$this->success("修改成功！");
				}else{
					$khyh=M('khyh');
					$id=I('get.id');
					$result=$khyh->find($id);				
					$this->assign('rs',$result);
					$this->display();
				}
		}
		function del_yh(){
			if($_GET){
				$id=I('id');
				$khyh=M('khyh');
				$khyh->where(array("id"=>$id))->delete();
				$this->success("删除成功！");
			}
		}
		function pt_yh(){
			if($_GET){
				$id=I('id');
				$Ptxq=M("Ptxq");
				$rs=$Ptxq->find($id);
				
				$khyh=M('khyh');
				$yh=$khyh->select();
				
				$pt_yh=M('pt_yh');
				$pt_yh=$pt_yh->table('cmf_pt_yh ptyh, cmf_khyh khyh')->where('ptyh.yh_id = khyh.id and ptyh.pt_id='.$id)->field('ptyh.*,khyh.name')->select();
	
				$this->assign('pt_yh',$pt_yh);
				$this->assign('yh',$yh);
				$this->assign('rs',$rs);
				$this->display();
			}
		}
		function add_pt_yh(){
			if($_POST){
				$data=I('post.');
				$pt_yh=M('pt_yh');
				$rs=$pt_yh->where($data)->find();
				if($rs==null){
					$pt_yh->add($data);
					$this->success("添加成功！");
				}else{
					
					$this->error("重复添加了！");
				}
				
			}
		}
			function del_pt_yh(){
			if($_GET){
				$id=I('id');
				$pt_yh=M('pt_yh');
				$pt_yh->where(array("id"=>$id))->delete();
				$this->success("删除成功！");
			}
		}
		function add_pt_yh_fj(){
			if($_POST){
				$pt_yh=M('pt_yh');
				$data=I('post.');
				$pt_id=$data['pt_id'];
				foreach($data as $k=>$v){
					if($k!='pt_id'){
						$arr[$k]=$v;
					}
				}
			
				foreach($arr as $k=>$v){
					$map['id']=$k;
					$map['yh_pdf']=$v;
					$pt_yh->save($map);
				}
				$this->success("添加成功！");
			}
		}
}